package com.compass.doctor.ui.prescription.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.LayoutPrescriptionDrugItemBinding
import com.compass.framework.adapter.BaseBindViewHolder
import com.compass.framework.adapter.BaseRecyclerViewAdapter
import com.compass.framework.ext.gone
import com.compass.framework.ext.onClick
import com.compass.framework.manager.DictionariesManager
import com.compass.framework.model.Drug

/**
 * Description： 处方药品adapter
 * Date:2023/12/28 10:32
 */
class PrescriptionDrugAdapter constructor(private var itemType: Int) :
    BaseRecyclerViewAdapter<Drug, LayoutPrescriptionDrugItemBinding>() {

    private var onCustomBottomClick: OnCustomButtonClick? = null


    fun setCustomBottomClick(onCustomBottomClick: OnCustomButtonClick?) {
        this.onCustomBottomClick = onCustomBottomClick
    }


    override fun onBindDefViewHolder(
        holder: BaseBindViewHolder<LayoutPrescriptionDrugItemBinding>,
        item: Drug?,
        position: Int
    ) {
        if (item == null) return
        holder.binding.apply {
            tvTitle.text = item.name
            if (item.specification.isNullOrEmpty()) {
                item.spec?.let { item.specification = it }

            }
            tvSpecification.text = "规格：${item.specification}"
            tvPackingUnit.text =item.packingUnit
            if (item.channelLabel.isEmpty()) {
                item.channelLabel = DictionariesManager.getChannel(item.channel)
            }
            if (item.frequencyLabel.isEmpty()) {
                item.frequencyLabel = DictionariesManager.getFrequency(item.frequency)
            }
            if (item.usageLabel.isEmpty()) {
                item.usageLabel = DictionariesManager.getUsage(item.usage)
            }


            if (item.type==1){
                tvUsage.text = "用法用量：${item.channelLabel}/${item.frequencyLabel}/${item.usageLabel} ${item.unitNum}${item.dosage}"
            }else{

                tvUsage.text = "备注：${item.getRemarkString()}"
            }

            tvNum.text = item.num
            if (itemType == 2) {
                tvPlusSign.gone()
                tvMinusSign.gone()
            }
            tvPlusSign.onClick {
                onCustomBottomClick?.onButtonCallBack(position, "plusSign")
            }
            tvMinusSign.onClick {
                onCustomBottomClick?.onButtonCallBack(position, "MinusSign")
            }
            tvDelete.onClick {
                onCustomBottomClick?.onButtonCallBack(position, "delete")
            }


        }
    }


    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): LayoutPrescriptionDrugItemBinding {
        return LayoutPrescriptionDrugItemBinding.inflate(layoutInflater, parent, false)
    }
}


